var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailLoaded: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var returnid = options.returnid;

    app.showLoading();
    this.loadReturnDetail(returnid);
  },
  // load return detail
  loadReturnDetail: function (returnid) {
    var url = '/index.php/api/Order/return_goods_info';
    var params = {
      id: returnid
    }
    app.request(url,params).then(this.returnDetailCallBack.bind(this));
  },
  // 回调
  returnDetailCallBack: function (res) {
    console.log(res);
    app.hideLoading();
    var details = {};
    details.id = res.result.return_goods.id;
    details.name = res.result.goods.goods_name;
    details.icon = app.globalData.webUrl + res.result.goods.original_img;
    details.matterdate = app.formatDateTime(res.result.return_goods.addtime);
    details.ordersn = res.result.return_goods.order_sn;
    details.price = res.result.return_goods.refund_money;
    details.statusVal = res.result.return_goods.status;
    details.status = app.globalData.returnStatus[res.result.return_goods.status];
    details.type = app.globalData.statusDesc[res.result.return_goods.type];
    details.isreceive = res.result.return_goods.is_receive;
    details.desc = res.result.return_goods.describe;
    details.reason = res.result.return_goods.reason;
    details.remark = res.result.return_goods.remark;
    details.buycount = res.result.return_goods.goods_num;
    details.imgs = res.result.return_goods.imgs;
    for (var i = 0; i < details.imgs.length; i++) {
      details.imgs[i] = app.globalData.webUrl + '/' + details.imgs[i];
    }
    this.setData({
      details: details, detailLoaded: true
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  showImageDetail: function (event) {
    var imgurl = event.currentTarget.dataset.imageurl;
    wx.previewImage({
      urls: this.data.details.imgs,
      current: imgurl
    });
  },
  cancelReturn: function () {
    var url = '/index.php/api/Order/return_goods_cancel';
    var params = {
      id: this.data.details.id
    };
    app.request(url,params).then(this.cancelReturnCallBack.bind(this));
  },
  cancelReturnCallBack: function (res) {
    wx.showToast({
      title: res.msg,
      icon: 'none'
    });
    if(res.status == 'ok'){
      var delayid = setTimeout(function(){
        clearTimeout(delayid);
        wx.navigateBack({});
      },1000);
    }
  }
})